﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Web.Configuration;
using System.Threading;

namespace spktNhom4DoAnCuoiKy.ManagerModules
{
    public partial class CreateSession : System.Web.UI.Page
    {
        AuctionOnlineDataContext db = new AuctionOnlineDataContext();
        void FillSanPhamData()
        {
            gvSanPham.DataSourceID = "";
            gvSanPham.DataSource = db.SanPhams.Select(x => new { x.MaSanPham, x.TenSanPham, x.GiaThamDinh, x.HinhAnh, x.MoTa, x.MoTaNguonGoc, x.SoLuong, x.TinhTrangSanPham1.MoTaTinhTrang }).ToList();
            gvSanPham.DataBind();
        }
        void FillThongTinServer()
        {
            var servers = db.Servers.Where(x => x.MaServer == WebConfigurationManager.AppSettings["WebServerCode"].ToString()).FirstOrDefault();
            if (servers != null)
            {
                lblDiaChiServer.Text = String.Format("{0} - {1}", servers.DiaChi, servers.TenQuocGia) ;
                lblMaServer.Text = String.Format("{0}_{1}", servers.VietTatTenQuocGia.ToUpper(), servers.SoThuTuTram);
                lblMoTaServer.Text = servers.MoTa;
                lblTenServer.Text = servers.TenServer;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            txtTimKiem.Attributes.Add("onkeypress", "return clickButton(event,'" + btnTimKiem.ClientID + "')");
            if (!IsPostBack) 
            {
                //load Nhóm Phiên
                hlTim.Attributes.Add("rel", "popuprel2");
                ddlNhomPhien.DataTextField = "TenNhomPhien";
                ddlNhomPhien.DataValueField = "MaNhomPhien";
                ddlNhomPhien.DataSource = db.NhomPhiens.Select(x => new { x.MaNhomPhien, TenNhomPhien = Session.getResourceManager().GetString(x.TenNhomPhien) }).ToList();
                ddlNhomPhien.DataBind();
                ///mặc định
                ddlNhomPhien.SelectedIndex = 0;

                string selectedvalue = ddlNhomPhien.SelectedValue;
                ddlLoaiPhien.DataTextField = "TenLoaiPhien";
                ddlLoaiPhien.DataValueField = "MaLoaiPhien";
                ddlLoaiPhien.DataSource = db.NhomLoaiPhiens.Where(x=>x.NhomPhienId==selectedvalue).Select(x => new { x.LoaiPhien.MaLoaiPhien, TenLoaiPhien = Session.getResourceManager().GetString(x.LoaiPhien.TenLoaiPhien) }).ToList();
                ddlLoaiPhien.DataBind();

                cblHinhThucVanChuyen.DataTextField = "MoTa";
                cblHinhThucVanChuyen.DataValueField = "MaHinhThuc";
                cblHinhThucVanChuyen.DataSource = db.HinhThucVanChuyens.Select(x => new { x.MaHinhThuc, MoTa = Session.getResourceManager().GetString(x.MoTa) }).ToList();
                cblHinhThucVanChuyen.DataBind();
                
            }
            FillSanPhamData();
            FillThongTinServer();
            Timer1.Enabled = false;
            
        }

        protected void ddlNhomPhien_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selectedvalue = ddlNhomPhien.SelectedValue;
            ddlLoaiPhien.DataSource = db.NhomLoaiPhiens.Where(x => x.NhomPhienId == selectedvalue).Select(x => new { x.LoaiPhien.MaLoaiPhien, TenLoaiPhien = Session.getResourceManager().GetString(x.LoaiPhien.TenLoaiPhien) }).ToList();
            ddlLoaiPhien.DataBind();
           
        }

        protected void gvSanPham_SelectedIndexChanged(object sender, EventArgs e)
        {
            int row = gvSanPham.SelectedRow.RowIndex;
            string masp = gvSanPham.Rows[row].Cells[2].Text;
            SanPham sp = db.SanPhams.Where(x => x.MaSanPham == masp).FirstOrDefault();
            if (sp != null)
            {
                txtSanPham.Text = sp.MaSanPham;
                lblMoTa.Text = sp.MoTa;
                lblSoLuong.Text = sp.SoLuong.ToString();
                lblTenSanPham.Text = sp.TenSanPham;
                lblXuatXu.Text = sp.MoTaNguonGoc;
                ImgHinhAnh.ImageUrl = "~/"+sp.HinhAnh;
            }
            else
            {
                txtSanPham.Text = "";
                lblMoTa.Text = "";
                lblSoLuong.Text = "";
                lblTenSanPham.Text = "";
                lblXuatXu.Text = "";
            }
            Timer1.Enabled = true;
            

            
        }

        protected void btnKiemTra_Click(object sender, EventArgs e)
        {
            aspnet_User user = db.aspnet_Users.Where(x => x.UserName == txtNguoiBan.Text.Trim()).FirstOrDefault();
            if (user != null)
            {
                lblThongBao.Text = "Hợp Lệ";
                lblThongBao.ForeColor = Color.DarkGreen;
            }
            else
            {
                lblThongBao.Text = "Không Hợp Lệ";
                lblThongBao.ForeColor = Color.Red;
            }
            lblThongBao.Visible = true;
        }

        protected void Timer1_Tick(object sender, EventArgs e)
        {

        }

        protected void btnTimKiem_Click(object sender, ImageClickEventArgs e)
        {

            gvSanPham.DataSourceID = "";
            gvSanPham.DataSource = db.SanPhams.Where(x => x.TenSanPham.Contains(txtTimKiem.Text) && x.isDangBan==false).Select(x => new { x.MaSanPham, x.TenSanPham,x.SoLuong,x.GiaThamDinh, x.HinhAnh, x.MoTa, x.MoTaNguonGoc, x.TinhTrangSanPham1.MoTaTinhTrang }).ToList();
            gvSanPham.DataBind();
        }

        protected void UpdatePanel1_Load(object sender, EventArgs e)
        {
            
        }

        protected void gvSanPham_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvSanPham.PageIndex = e.NewPageIndex;
            gvSanPham.DataBind();
        }

        protected void txtNguoiBan_TextChanged(object sender, EventArgs e)
        {
            lblThongBao.Visible = false;
        }

        protected void btnTaoMoi_Click(object sender, EventArgs e)
        {
            
            
           
              
                PhienDauGia phien = new PhienDauGia();
                phien.MaPhien = db.PhienDauGias.GetNewCode<PhienDauGia>("MaPhien", "P");
                phien.MaSanPham = txtSanPham.Text;
                NhomLoaiPhien nl = db.NhomLoaiPhiens.Where(x => x.LoaiPhienId == ddlLoaiPhien.SelectedValue && x.NhomPhienId == ddlNhomPhien.SelectedValue).FirstOrDefault();
                phien.ServerPhien = WebConfigurationManager.AppSettings["WebServerCode"].ToString();
                phien.NhomLoaiPhienId = nl.NhomLoaiPhienId;
                phien.TenNguoiDangTin = txtNguoiBan.Text;
                phien.ThoiGianBatDauBan = txtThoiGianBatDau.Value;
                phien.ThoiGianKetThucBan = txtThoiGianKetThuc.Value;
                phien.ThoiGianHuyPhien = txtThoiGianHuyPhien.Value;
                phien.GiaSan = (double)txtGiaSan.GetCurrencyAmount();
                phien.GiaTran = (double)txtGiaTran.GetCurrencyAmount();
                phien.BuocGia = (double)txtBuocGia.GetCurrencyAmount();
                phien.GiaThang = phien.GiaSan;



                foreach (ListItem ht in cblHinhThucVanChuyen.Items)
                {
                    if (ht.Selected)
                    {
                        HinhThucVanChuyenHoTro htht = null;

                        if (db.HinhThucVanChuyenHoTros.Any(x => x.MaPhien == phien.MaPhien && x.MaHinhThucVanChuyen == ht.Value))
                        {
                            htht = db.HinhThucVanChuyenHoTros.Where(x => x.MaPhien == phien.MaPhien && x.MaHinhThucVanChuyen == ht.Value).FirstOrDefault();

                        }
                        else
                        {
                            htht = new HinhThucVanChuyenHoTro();
                            htht.MaHinhThucVanChuyen = ht.Value;
                            htht.MaPhien = phien.MaPhien;

                        }
                        phien.HinhThucVanChuyenHoTros.Add(htht);

                    }
                }
                db.PhienDauGias.InsertOnSubmit(phien);

                db.SubmitChanges();
                SanPham sp = db.SanPhams.Where(x => x.MaSanPham == phien.MaSanPham).FirstOrDefault();
                if (sp != null)
                {
                    sp.isDangBan = true;
                    db.SubmitChanges();
                }
                //Thông báo
                Page.RegisterStartupScript("MyKey", "inform();");
                //reset text
                txtBuocGia.Text = "";
                txtGiaSan.Text = "";
                txtGiaTran.Text = "";
                txtNguoiBan.Text = "";
                txtSanPham.Text = "";
                txtThoiGianBatDau.Text = "";
                txtThoiGianHuyPhien.Text = "";
                txtTimKiem.Text = "";
                //ScriptManager.RegisterStartupScript(btnTaoMoi, btnTaoMoi.GetType(), "", "DoPostBack(this)", true);
               
           
        }

        protected void btnHuy_Click(object sender, EventArgs e)
        {
            txtBuocGia.Text = "";
            txtGiaSan.Text = "";
            txtGiaTran.Text = "";
            txtNguoiBan.Text = "";
            txtSanPham.Text = "";
            txtThoiGianBatDau.Text = "";
            txtThoiGianHuyPhien.Text = "";
            txtTimKiem.Text = "";
            //ScriptManager.RegisterStartupScript(btnHuy,this.GetType(), "MyKey", "inform();", true);
            
        }
      
        
        protected void UpdatePanelTaoMoi_Load(object sender, EventArgs e)
        {
            
        }

        protected void btnKiemTraNgay_Click(object sender, EventArgs e)
        {
            if (txtThoiGianBatDau.Value == DateTime.MinValue || txtThoiGianHuyPhien.Value == DateTime.MinValue || txtThoiGianKetThuc.Value == DateTime.MinValue)
            {
                lblLoiNgay.Text = "Vui lòng nhập ngày trước.";
                lblLoiNgay.Visible = true;
                Thread.Sleep(2000);
                lblLoiNgay.Visible = false;

            }
            else
            {
                if (txtThoiGianBatDau.Value <= txtThoiGianHuyPhien.Value && txtThoiGianHuyPhien.Value < txtThoiGianKetThuc.Value && txtThoiGianBatDau.Value > DateTime.Now)
                {
                    lblLoiNgay.Text = "Thời gian hợp lệ";
                    lblLoiNgay.ForeColor = Color.Green;
                    lblLoiNgay.Visible = true;
                    Thread.Sleep(3000);
                    lblLoiNgay.Visible = false;
                }
                else
                {
                    lblLoiNgay.Text = "Thời gian không hợp lệ:<br/>Thời gian bắt đầu <= Thời gian hủy phiên < Thời gian kêt thúc";
                    lblLoiNgay.ForeColor = Color.Green;
                    lblLoiNgay.Visible = true;
                    Thread.Sleep(3000);
                    lblLoiNgay.Visible = false;
                    
                }
            }
        }

        protected void btnKiemTraGia_Click(object sender, EventArgs e)
        {
          
            if (true)
            {
                lblLoiNgay.Text = "Vui lòng nhập ngày trước.";
                lblLoiNgay.Visible = true;
                Thread.Sleep(2000);
                lblLoiNgay.Visible = false;

            }
            else
            {
                if (txtThoiGianBatDau.Value <= txtThoiGianHuyPhien.Value && txtThoiGianHuyPhien.Value < txtThoiGianKetThuc.Value && txtThoiGianBatDau.Value > DateTime.Now)
                {
                    lblLoiNgay.Text = "Thời gian hợp lệ";
                    lblLoiNgay.ForeColor = Color.Green;
                    lblLoiNgay.Visible = true;
                    Thread.Sleep(3000);
                    lblLoiNgay.Visible = false;
                }
                else
                {
                    lblLoiNgay.Text = "Thời gian không hợp lệ:<br/>Thời gian bắt đầu <= Thời gian hủy phiên < Thời gian kêt thúc";
                    lblLoiNgay.ForeColor = Color.Green;
                    lblLoiNgay.Visible = true;
                    Thread.Sleep(3000);
                    lblLoiNgay.Visible = false;

                }
            }
        }
    }
}